﻿using CPPEI.Coolzon.Designtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.UI.XProperty.Editors
{
    /// <summary>
    /// Interaction logic for ResourceFileEditWindow.xaml
    /// </summary>
    public partial class ResourceFileEditWindow : Window
    {
        public ResourceFile RetResourceFile { get; private set; }
        public ResourceFileEditWindow(ResourceFile retResourceFile = null)
        {
            InitializeComponent();
            RetResourceFile = retResourceFile;
        }

        private void cbPicName_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbPicName.SelectedItem != null)
            {
                ImageRes imageRes = cbPicName.SelectedItem as ImageRes;
                Uri uri = new Uri(imageRes.FullPath, UriKind.Absolute);

                RetResourceFile = new ResourceFile(SolutionResourceManager.GetRelativeFilePath(imageRes.FullPath));

                _ctlPreview.Stop();
                _ctlPreview.Source = uri;
                _ctlPreview.Play();
            }
            else
            {
                RetResourceFile = null;
                _ctlPreview.Source = null;
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Uri fullPath = null;
            if (RetResourceFile != null && !string.IsNullOrWhiteSpace(RetResourceFile.Path))
            {
                fullPath = new Uri(SolutionResourceManager.GetAbsoluteFilePath(RetResourceFile.Path), UriKind.Absolute);
            }
            ImageRes selected = null;
            ImageRes[] imageList = ResPoolFactory.GetImageResPool().GetVideos();
            if (imageList != null && imageList.Length > 0)
            {
                foreach (var item in imageList)
                {
                    cbPicName.Items.Add(item);
                    if (fullPath != null && fullPath.AbsolutePath == new Uri(item.FullPath).AbsolutePath)
                    {
                        selected = item;
                    }
                }
                if (selected != null)
                {
                    cbPicName.SelectedItem = selected;
                }
            }
        }

        private void _ctlBtnOK_Click_1(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
        }

        private void _ctlClear_Click(object sender, RoutedEventArgs e)
        {
            cbPicName.SelectedItem = null;
        }
    }
}
